package com.example.springbootNetty.netty.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springbootNetty.cache.ChannelRepository;
import com.example.springbootNetty.entity.SysUser;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: Kevin
 * @Date: 2022/2/9 11:28
 */
@Component
@ChannelHandler.Sharable
public class NettyRegistHandler implements ChannelInboundHandler {
    @Autowired
    private ChannelRepository channelRepository;

    @Override
    public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        System.out.println("接收到" + channelHandlerContext.channel().id() + "的消息:" + o);
        JSONObject jsonObject = null;
        try {
            jsonObject = JSON.parseObject(o.toString());
            o = jsonObject;
        } catch (Exception e) {
            channelHandlerContext.channel().write("错误的格式\r\n");
            channelHandlerContext.channel().flush();
            return;
        }
        if (!"/login/login".equals(jsonObject.getString("url"))) {
            if (channelRepository.getUsername(channelHandlerContext.channel().id()) == null){
                channelHandlerContext.channel().write("请先登录\r\n");
                channelHandlerContext.channel().flush();
                return;
            }
        } else {
            SysUser sysUser = jsonObject.getObject("sysUser",SysUser.class);
            // TODO check the user from database
            channelRepository.putUser(channelHandlerContext.channel().id(),sysUser.getUsername());
            channelRepository.putChannel(sysUser.getUsername(),channelHandlerContext);
            channelHandlerContext.channel().write("登录成功\r\n");
            channelHandlerContext.channel().flush();
            return;
        }
        channelHandlerContext.fireChannelRead(o);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {

    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception {

    }
}
